package com.ruoyi.ini.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ini.domain.XlkjIni;
import com.ruoyi.ini.service.IXlkjIniService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.DictStrUtils;
import com.ruoyi.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 事件Controller
 *
 * @author wang
 * @date 2022-02-08
 */
@RestController
@RequestMapping("/app/ini/ini")
public class XlkjIniControllerApp extends BaseController
{
    @Autowired
    private IXlkjIniService xlkjIniService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询事件列表
     */
    @GetMapping("/list")
    public TableDataInfo list(XlkjIni xlkjIni)
    {
        startPage();
        if(xlkjIni.getDepartmentId() != null && xlkjIni.getDepartmentId() != ""){
            List<String> list = sysDeptMapper.selectIdByParentId(Long.valueOf(xlkjIni.getDepartmentId()));
            String str = "(";
            for(int i =0;i<list.size();i++){
                str += list.get(i);
                if(i != list.size()-1){
                    str += ",";
                }
            }
            str += ")";
            xlkjIni.setDepartmentList(str);
        }

        if(xlkjIni.getStatus().equals("1")){
            xlkjIni.setStatus(null);
            xlkjIni.setAaa("aaa");
        }

        startPage();
        List<XlkjIni> list = xlkjIniService.selectXlkjIniList(xlkjIni);
        for(XlkjIni ini:list){
            if(ini.getImg() != null && ini.getImg().length()>0){
                ini.setImg(UrlUtils.getUrl() + ini.getImg());
            }
            //字典翻译
            ini.setType(DictUtils.getDictLabel(DictStrUtils.INCIDENT_TYPE,ini.getType()));
        }
        return getDataTable(list);
    }

    /**
     * 导出事件列表
     */
    @PreAuthorize("@ss.hasPermi('ini:ini:export')")
    @Log(title = "事件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(XlkjIni xlkjIni)
    {
        List<XlkjIni> list = xlkjIniService.selectXlkjIniList(xlkjIni);
        ExcelUtil<XlkjIni> util = new ExcelUtil<XlkjIni>(XlkjIni.class);
        return util.exportExcel(list, "事件数据");
    }

    /**
     * 获取事件详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        XlkjIni ini = xlkjIniService.selectXlkjIniById(id);
        if(ini.getImg() != null && ini.getImg().length() > 0){
            ini.setImg(UrlUtils.getUrl() + ini.getImg());
        }
        if(ini.getAudio() != null && ini.getAudio().length() > 0){
            ini.setAudio(UrlUtils.getUrl() + ini.getAudio());
        }
        //字典值翻译
        ini.setRiskType(DictUtils.getDictLabel(DictStrUtils.RISK2_TYPE,ini.getRiskType()));
        //查询超时状态
        Long status = xlkjIniService.selectTimeStatus(id);
        if(status > 0){
            ini.setSuperviseStatus("Y");
        }else {
            ini.setSuperviseStatus("N");
        }
        return AjaxResult.success(ini);
    }

    /**
     * 新增事件
     */
    @PostMapping
    public AjaxResult add(@RequestBody XlkjIni xlkjIni)
    {
        //判断是否为立即督办
        if(xlkjIni.getImmediately().equals("0")){
            xlkjIni.setFinishTime(new Date(new Date().getTime() - 86400000));
        }
        //设置待办理状态
        xlkjIni.setStatus("1");
        //是否需要协助值
        xlkjIni.setAssist("N");
        //通过phone设置userId
        SysUser sysUser = sysUserService.selectUserByUserName(xlkjIni.getPhone());
        xlkjIni.setUserId(sysUser.getUserId());
        return toAjax(xlkjIniService.insertXlkjIni(xlkjIni));
    }

    /**
     * 修改事件
     */
    @PutMapping
    public AjaxResult edit(@RequestBody XlkjIni xlkjIni)
    {
        //如果是修改状态为（审批通过），需要先看看该事件是否超时，如果超时，则需要督办部门进行督办，也就是将 status = 7
        XlkjIni ini = xlkjIniService.selectXlkjIniById(xlkjIni.getId());
        if(xlkjIni.getStatus()!=null && xlkjIni.getStatus().equals("2") && ini.getFinishTime()!=null){
            //查询是否超时
            Long status = xlkjIniService.selectTimeStatus(xlkjIni.getId());
            if(status > 0){
                xlkjIni.setStatus("7");
            }
        }
        return toAjax(xlkjIniService.updateXlkjIni(xlkjIni));
    }

    /**
     * 删除事件
     */
    @PreAuthorize("@ss.hasPermi('ini:ini:remove')")
    @Log(title = "事件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xlkjIniService.deleteXlkjIniByIds(ids));
    }
}
